package com.cpf.servlet;

import java.io.IOException;
import java.io.PrintWriter;


import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import com.dao.ObjectDao;
import com.pro.bean.Info;
import java.util.List;

import com.cpf.bean.Nodes;
import com.pro.bean.Card_View;
import com.cpf.dao.ViewDao;



public class ViewServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public ViewServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
        doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("show in viewservlet");
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/xml;charset=utf-8");
		PrintWriter out = response.getWriter();
		String cmp=new String(request.getParameter("cmp").getBytes("ISO-8859-1"),"UTF-8");
		System.out.println("cmp"+cmp);
		String department=new String(request.getParameter("department").getBytes("ISO-8859-1"),"UTF-8");//
		System.out.println("dept"+department);//
		ViewDao dao= new ViewDao();
		//ArrayList list= dao.GetInfoByCmp(cmp);
		ArrayList list= dao.GetInfoByAll(department, cmp);//
		System.out.print(list.size());
		if (list != null && list.size() > 0) {
			Set<Nodes> s = new HashSet<Nodes>();
			//ArrayList<Nodes> nodelist = new ArrayList<Nodes>();
			for(int i=0;i<list.size();++i)
			{
				Card_View info = (Card_View)list.get(i);
				Nodes cmpnode = new Nodes();
				if(info.getCmp() == null)
					info.setCmp("");
				cmpnode.setNodeName(info.getCmp());
				cmpnode.setNodeId((i+1)+"");
				cmpnode.setParentId(-1+"");
				System.out.println(s.add(cmpnode));
			}
			for(int i=0;i<list.size();++i)
			{
				Card_View info = (Card_View)list.get(i);
				if(info.getDept() == null)
					info.setDept("");
				Nodes tmpnode  = new Nodes();
				tmpnode.setNodeName(info.getCmp());
				Nodes deptnode = new Nodes();
				String parentid = null;
				for(Nodes n:s)
				{
					if(n.equals(tmpnode))
						parentid = n.getNodeId();
				}
				deptnode.setNodeId(parentid+""+(i+1));
				deptnode.setParentId(parentid);
				deptnode.setNodeName(info.getDept());
				s.add(deptnode);
			}
			for(int i=0;i<list.size();++i)
			{
				Card_View info = (Card_View)list.get((i));
				if(info.getFirstname() == null)
					info.setFirstname("");
				Nodes tmpnode  = new Nodes();
				tmpnode.setNodeName(info.getDept());
				Nodes usernode = new Nodes();
				String parentid = null;
				for(Nodes n:s)
				{
					if(n.equals(tmpnode))
						parentid = n.getNodeId();
				}
				usernode.setNodeId(parentid+""+(i+1));
				usernode.setParentId(parentid);
				usernode.setNodeName(info.getFirstname());
				s.add(usernode);
			}
			out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
			out.println("<nodes>");
			System.out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
			System.out.println("<nodes>");
			/*for(Nodes node:s)
			{
				
				out.println("<node nodeId='" + node.getNodeId()
						+ "' parentId='" + node.getParentId()
						//+ "' hrefAddress='" + node.getHrefAddress() 
						+ "'>"
						+ node.getNodeName() + "</node>");
			}*/
			
			Iterator<Nodes> iterator=s.iterator();
			while(iterator.hasNext())
			{
				Nodes outnode=iterator.next();
				out.println("<node nodeId='" + outnode.getNodeId()
						+ "' parentId='" + outnode.getParentId()
						+ "' hrefAddress='" + "null" 
						+ "'>"
						+ outnode.getNodeName() + "</node>");
				System.out.println("<node nodeId='" + outnode.getNodeId()
						+ "' parentId='" + outnode.getParentId()
						+ "' hrefAddress='" + "null"
						+ "'>"
						+ outnode.getNodeName() + "</node>");
			}
			out.println("</nodes>");
			System.out.println("</nodes>");
			//RequestDispatcher dispatcher= request.getRequestDispatcher("../cpfpage/showview.jsp");
			//dispatcher.forward(request, response);
			/*
			out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
			out.println("<nodes>");
			for (int i = 0; i < list.size(); i++) {
				Info info = (Info) list.get(i);
				System.out.println(info.getCmp()+ info.getDept()+ info.getFirstName());
				out.println(info.getCmp());
				out.println(info.getDept());
				out.println(info.getFirstName());
			}*/
		
			
		
		}
	}
}		
		
		
		/*ArrayList<Nodes> list = DaoTest.getNodeInfo();
		if (list != null && list.size() > 0) {
			out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
			out.println("<nodes>");
			for (int i = 0; i < list.size(); i++) {
				Nodes node = list.get(i);
				out.println("<node nodeId='" + node.getNodeId()
						+ "' parentId='" + node.getParentId()
						+ "' hrefAddress='" + node.getHrefAddress() + "'>"
						+ node.getNodeName() + "</node>");
			}
			out.println("</nodes>");
		}*/
		



